package com.fs.pandaFund.common.util;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import darabonba.core.client.ClientOverrideConfiguration;
import java.util.concurrent.CompletableFuture;

/**
 * Author:HOU
 * date:2023/2/8  10:16
 * 案例:短信验证工具
 **/
public class SmsUtil {

    public static String SMSVerification(String phone) throws Exception{
        long rand = (long)(Math.random()*900000+100000);
        String code = String.valueOf(rand);
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("LTAI5t66NAtowktAcMDrHXYX")
                .accessKeySecret("fzdgZubA8OWXrUmIjASk1vobJSAfkG")
                .build());
        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers(phone)
                .signName("侯建荣个人博客")
                .templateCode("SMS_269245622")
                .templateParam("{\"code\":\""+code+"\"}")
                .build();
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = response.get();
//        System.out.println(new Gson().toJson(resp));
        client.close();
        return code;
    }

    public static void main(String[] args) throws Exception {
        String s = SMSVerification("18283930186");
    }

}
